
/*
This file is part of MJIN2.

Copyright (C) 2012-2013 Opensource Game Studio

This software is provided 'as-is', without any express or implied
warranty.  In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
   claim that you wrote the original software. If you use this software
   in a product, an acknowledgment in the product documentation would be
   appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
   misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/

#include <mjin2/priv/RegExp.h>

namespace mjin2
{
namespace priv
{

RegExp::RegExp() throw () : mRE(0) { }

RegExp::RegExp(const String &pattern,
               bool         caseSensitive) throw (Exception) : mRE(0)
{
    setPattern(pattern, caseSensitive);
}

RegExp::~RegExp() throw ()
{
    freeRE();
}

bool RegExp::match(const String &string,
                   StringList   *matchList) throw (Exception)
{
    if (!exec(string))
        return false;
    if (matchList)
    {
        s32 captureCount = 0;
        pcre_fullinfo(mRE, 0, PCRE_INFO_CAPTURECOUNT, &captureCount);
        for (s32 i = 1; i <= captureCount; ++i)
            matchList->push_back(string.substr(mOVec[2 * i],
                                               mOVec[2 * i + 1] - mOVec[2 * i]));
    }
    return true;
}

String RegExp::replace(const String &string,
                       const String &replacement,
                       bool         all) throw (Exception)
{
    String s = string;
    bool proceed = true;
    while (proceed && exec(s))
    {
        s.replace(mOVec[0], mOVec[1] - mOVec[0], replacement);
        proceed = all;
    }
    return s;
}

StringList RegExp::split(const String &string) throw (Exception)
{
    StringList o;
    String s = string;
    while (exec(s))
    {
        String ss = s.substr(0, mOVec[0]);
        if (ss.length())
            o.push_back(ss);
        s.erase(0, mOVec[1]);
    }
    // Remaining part after the pattern.
    if (s.length())
        o.push_back(s);
    return o;
}

void RegExp::setPattern(const String &pattern,
                        bool         caseSensitive) throw (Exception)
{
    const char *error;
    s32 errorOffset;
    s32 options = 0;
    if (!caseSensitive)
        options |= PCRE_CASELESS;
    freeRE();
    mRE = pcre_compile(pattern.c_str(), options, &error, &errorOffset, 0);
    if (!mRE)
        MJIN2_EXCEPTION("Failed to compile regular expression",
                        MJIN2_STR("Offset: %u Error: %s",
                                  errorOffset,
                                  error));
}

bool RegExp::exec(const String &string) throw (Exception)
{
    s32 rc = pcre_exec(mRE,
                       0,
                       string.c_str(),
                       string.length(),
                       0,
                       0,
                       mOVec,
                       MAX_REG_EXP_CAPTURES);
    if (rc < 0)
        return false;
    if (rc == 0)
        MJIN2_EXCEPTION(
            "MAX_REG_EXP_CAPTURES is too small",
            MJIN2_STR("Not enough room for %u captured strings", rc / 3 - 1));
    return true;
}

void RegExp::freeRE() throw ()
{
    if (mRE)
    {
        pcre_free(mRE);
        mRE = 0;
    }
}

} // namespace priv
} // namespace mjin2

